$(document).ready(function() {	
	$("#frmPerson").validate({
		errorClass		: 'errorMessage',
		errorElement	: 'span',
		onfocusout		: false,
		onkeyup			: false,
		rules: {
			pusername:{
				required 			: true,
				rangelength			: [5,30],
				validateFormat		: true,
				validateUsername	: true,
				validateBlacklist	: true
			},
			peducation:{
				required 			: true
			},
			peducation_type:{
				required 			: true
			},
			poccupation:{
				required 			: true
			},
			poccupation_type:{
				required 			: true
			},
			pposition:{
				required 			: true
			},
			pposition_type:{
				required 			: true
			},
			pmarital:{
				required 			: true
			},
			pmarital_type:{
				required 			: true
			},
			preligion:{
				required 			: true
			},
			preligion_type:{
				required 			: true
			},
			pintroduce:{
				required 			: true,
				minlength			: 10
			}
		},
		messages: {
			pusername:{
				required 			: username_required,
				rangelength			: username_range,
				validateFormat		: username_format,
				validateUsername	: username_validate,
				validateBlacklist	: username_blacklist
			},
			peducation:{
				required 			: education_required
			},
			peducation_type:{
				required 			: education_type_required
			},
			poccupation:{
				required 			: occupation_required
			},
			poccupation_type:{
				required 			: occupation_type_required
			},
			pposition:{
				required 			: position_required
			},
			pposition_type:{
				required 			: position_type_required
			},
			pmarital:{
				required 			: marital_required
			},
			pmarital_type:{
				required 			: marital_type_required
			},
			preligion:{
				required 			: religion_required
			},
			preligion_type:{
				required 			: religion_type_required
			},
			pintroduce:{
				required 			: introduce_required,
				minlength			: introduce_minlength
			}
		},
		errorPlacement: function(label, element) {			
			var id = element.attr('id');			
			label.insertAfter($('#' + id));			
		},
		submitHandler: function(form) {				
			form.submit();
		}
	});	
	$.validator.addMethod('validateFormat', function() {
		var strUsername = $('#pusername').val();
		return validateUsername(strUsername);
	});
	$.validator.addMethod('validateUsername', function(){	
		var result 		= true;
		var strUsername	= $('#pusername').val();
		var strUrl 		= HOST_VFRIEND + 'valid/isvalidusername';
		$.ajax({
			cache	: false,
            async	: false,
            type	: 'POST',
			data	: 'username=' + strUsername,
			url		: strUrl,
			success	: function(response) {
				result = (response == 1) ? true : false;
			}
		});
		return result;
	});
	$.validator.addMethod('validateBlacklist', function(){											
		return true;											
	});
	
	/** 
	 * Character counter for textarea
	 */  
	$('#pintroduce').each(function(){  
		var length = $(this).val().length;		
		$(this).parent().find('#introCount').html( length + ' ' + characters);  
		$(this).keyup(function(){  			
			var new_length = $(this).val().length; 
			$(this).parent().find('#introCount').html( new_length + ' ' + characters);  
		});  
		
	});  
	$('#pusername').each(function(){   
		var strUserName = $(this).val();   
		$(this).parent().find('#urlFriendly').html( HOST_VFRIEND +  strUserName);
		$(this).keyup(function(){   
			var strUserNameNw = $(this).val();  
			$(this).parent().find('#urlFriendly').html( HOST_VFRIEND + strUserNameNw);  
		});  
		$(this).blur(function(){
			var strUserNameNw = $(this).val();  
			$(this).parent().find('#urlFriendly').html( HOST_VFRIEND + strUserNameNw);  
		});
	});
	
});
personalOther = function(value, container) {
	if(value == -1) {
		$('#' + container).show();
	}
	else {
		$('#' + container).hide();
	}
}